package com.demo.event.listener;

import com.demo.global.LimitedPlayerData;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerMoveEvent;

/**
 * 登录相关事件监听类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/05/02 下午 07:25
 */
public class LoginListener implements Listener {

    private static void limitUnLoginPlayers(String playerName, Cancellable c) {
        if (LimitedPlayerData.hasPlayer(playerName)) {
            //玩家在限制名单里，则限制其操作
            c.setCancelled(true);
        }
    }


    /**
     * 监听玩家登录
     *
     * @param e 玩家登录事件
     */
    @EventHandler
    public void onUserLogin(PlayerLoginEvent e) {
        //未登录玩家加入限制列表
        LimitedPlayerData.addPlayer(e.getPlayer().getName());
    }



    @EventHandler
    public void limitMove(PlayerMoveEvent e) {
        //限制移动
        limitUnLoginPlayers(e.getPlayer().getName(), e);
    }
    @EventHandler
    public void limitInteract(PlayerInteractEvent e) {
        //限制交互
        limitUnLoginPlayers(e.getPlayer().getName(), e);
    }
    @EventHandler
    public void limitInventoryOpen(InventoryOpenEvent e) {
        //限制打开物品栏
        limitUnLoginPlayers(e.getPlayer().getName(), e);
    }



}
